package service.impl;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import model.Product;
import service.IProductService;

import java.util.Date;

/**
 * Created by ykj on 16/1/8.
 */
public class ProductServiceImpl implements IProductService {
    public Page findByPage(int page, int pageSize) {
        return Product.dao.paginate(page, pageSize, "select p.*,t.name type_name from product p inner join type t on p.type_id = t.id");

    }

    public Model save(Model model) {
        Product product = (Product) model;
        product.setAlreadyMoney(0d);
        product.setCreateTime(new Date());
        boolean result = product.save();
        return result ? product : null;
    }

    public Model update(Model model) {
        Product product = (Product) model;
        if(product.getTotalMoney()<product.getAlreadyMoney()) return null;
        product.setCreateTime(Product.dao.findById(product.getId()).getCreateTime());
        boolean result = product.update();
        return result ? product : null;
    }

    public boolean delete(int id) {
        return Product.dao.deleteById(id);
    }

    public Model findById(int id) {
        return Product.dao.findById(id);
    }

    public Page<Product> searchProducts(int page, int pageSize, String orderBy) {
        return Product.dao.paginate(page, pageSize, "select * from product order by id desc");
    }

    public Page<Product> searchProductsByQuery(int page, int pageSize, String query, String orderBy) {
//        String sql = "select * from product where name like '%"+query+"%'" +
//                " or need_time like '%"+query+"%'" +
//                " or rate_return like '%"+query+"%'" +
//                " order by id desc";
        String param = "%"+query+"%";
        String sql = "select * from product where name like ? or need_time like ?  or rate_return like ? order by id desc";
        return Product.dao.paginate(page, pageSize, sql, param, param, param);
    }

    public Page<Product> filterProducts(int page, int pageSize, String rateSpan, String timeSpan, String type) {
        String sql = "select * from product where rate_return BETWEEN ? and ? and need_time BETWEEN ? and ? ";
        int rateStart = 0;
        int rateEnd = 100;
        int timeStart = 0;
        int timeEnd = 9999;
        if(rateSpan != null){
            String[] r = rateSpan.split(":");
            rateStart = Integer.valueOf(r[0]);
            rateEnd = Integer.valueOf(r[1]);
        }
        if(timeSpan != null){
            String[] t = timeSpan.split(":");
            timeStart = Integer.valueOf(t[0]);
            timeEnd = Integer.valueOf(t[1]);
        }
        System.out.println(rateStart+":"+rateEnd+":"+timeStart+":"+timeEnd);
        if(type != null){
            sql = sql + " and type_id = ? order by id desc";
            return Product.dao.paginate(page, pageSize, sql, rateStart, rateEnd, timeStart, timeEnd, type);
        }else {
            sql = sql + " order by id desc";
            return Product.dao.paginate(page, pageSize, sql, rateStart, rateEnd, timeStart, timeEnd);
        }
    }
}
